﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Promoter : WebPageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HiddenFieldUserId.Value = base.UserSystem.Id.ToString();
            GridViewInterview.DataSource = GetListFilterInterviews("IdInterview", "Ascending");
            GridViewInterview.DataBind();

            //Actualizacion de estados solo para Administradores
            ButtonUpdateInterview.Visible = (base.UserSystem.RoleId == 1);

        }
    }


    protected void GridViewInterview_DataBound(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridViewInterview.Rows)
        {
            Image img = (Image)row.FindControl("imgStatus");
            img.ImageUrl = GetStatusColor(row.Cells[7].Text.ToString());
            row.Cells[7].Text = GetStatus(row.Cells[7].Text);
        }
    }

    private String GetStatus(string status)
    {
        if (status.Equals("0") || String.IsNullOrEmpty(status) || status.Equals("&nbsp;")) return "Pendiente";
        if (status.Equals("1")) return "Paso";
        if (status.Equals("2")) return "No paso";

        return "Pendiente";

    }

    private String GetStatusColor(string status)
    {
        Image imagen = new Image();

        if (status.Equals("0") || String.IsNullOrEmpty(status) || status.Equals("&nbsp;"))
        {
            imagen.ImageUrl = "~/Image/ic-Naranja.gif";
        }
        else if (status.Equals("1"))
        {
            imagen.ImageUrl = "~/Image/ic-green.jpg";
        }
        else
        {
            imagen.ImageUrl = "~/Image/ic-Rojo.gif";
        }

        return imagen.ImageUrl;
    }
    
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        GridViewInterview.DataSource = GetListFilterInterviews("IdInterview", "Ascending");
        GridViewInterview.PageIndex = 0;
        GridViewInterview.DataBind();
    }

    protected void ButtonUpdateInterview_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridViewInterview.Rows)
        {
            HiddenField HiddenFieldIdInterview = (HiddenField)row.FindControl("HiddenFieldIdInterview");
            int idInterview = int.Parse(HiddenFieldIdInterview.Value);
            CheckBox CheckBoxCapacitation = (CheckBox)row.FindControl("CheckBoxCapacitation");
            CheckBox CheckBoxContract = (CheckBox)row.FindControl("CheckBoxContract");

            Interview interview = db.Interviews.First(u => u.IdInterview == idInterview);
            interview.Training = CheckBoxCapacitation.Checked;
            interview.Hired = CheckBoxContract.Checked;

            db.SubmitChanges();

        }

        GridViewInterview.DataSource = GetListFilterInterviews(HiddenFieldSortExpression.Value, HiddenFieldSortDirection.Value);
        GridViewInterview.DataBind();
    }

    List<Interview> GetListFilterInterviews(String SortExpression, String SortDirection)
    {
        string filter = string.Empty;
        List<Interview> ListInterviews = db.Interviews.ToList();

        if (!String.IsNullOrEmpty(TextBoxName.Text.Trim()))
        {
            ListInterviews = ListInterviews.FindAll(delegate(Interview i) { 
                                                        return  (i.PromoterName!=null && i.PromoterName.ToUpper().Contains(TextBoxName.Text.ToUpper())) ||
                                                                (i.PromoterSurname1 != null && i.PromoterSurname1.ToUpper().Contains(TextBoxName.Text.ToUpper())) ||
                                                                (i.PromoterSurname2 != null && i.PromoterSurname2.ToUpper().Contains(TextBoxName.Text.ToUpper()));
            });
        }
        if (!String.IsNullOrEmpty(TextBoxNumberDocument.Text.Trim()))
        {
            ListInterviews = ListInterviews.FindAll(delegate(Interview i)
            {
                return i.PromoterDNI.Contains(TextBoxNumberDocument.Text);
            });
        }
        if (!String.IsNullOrEmpty(TextBoxRegisterDate.Text.Trim()))
        {
            IFormatProvider culture = new System.Globalization.CultureInfo("ES-PE", true);
            DateTime startDate = DateTime.Parse(TextBoxRegisterDate.Text, culture, System.Globalization.DateTimeStyles.AssumeLocal);
            DateTime startEnd = startDate.AddDays(1);
            ListInterviews = ListInterviews.FindAll(delegate(Interview i)
            {
                return i.RegisterDate >= startDate && i.RegisterDate < startEnd;
            });
        }

        switch (base.UserSystem.RoleId.Value)
        {
            case 2: ListInterviews = ListInterviews.FindAll(delegate(Interview i){ return i.LiderId == base.UserSystem.Id; }); break;
            case 3: ListInterviews = ListInterviews.FindAll(delegate(Interview i) { return i.CoordinatorId == base.UserSystem.Id; }); break;
            default: break;
        }

        ListInterviews.Sort(delegate(Interview i1, Interview i2)
        {
            return String.Compare(i1.PromoterName + " " + i1.PromoterSurname1 + " " + i1.PromoterSurname2,
                                  i2.PromoterName + " " + i2.PromoterSurname1 + " " + i2.PromoterSurname2);
        });

        if (SortExpression.Equals("PromoterName"))
        {
            if (HiddenFieldSortDirection.Value.Equals("Ascending"))
            {
                ListInterviews.Sort(delegate(Interview i1, Interview i2)
                {
                    return String.Compare(i1.PromoterName + " " + i1.PromoterSurname1 + " " + i1.PromoterSurname2,
                                          i2.PromoterName + " " + i2.PromoterSurname1 + " " + i2.PromoterSurname2);
                });
            }
            else
            {
                ListInterviews.Sort(delegate(Interview i2, Interview i1)
                {
                    return String.Compare(i1.PromoterName + " " + i1.PromoterSurname1 + " " + i1.PromoterSurname2,
                                          i2.PromoterName + " " + i2.PromoterSurname1 + " " + i2.PromoterSurname2);
                });
            }
        }

        if (SortExpression.Equals("IdInterview"))
        {
            if (HiddenFieldSortDirection.Value.Equals("Ascending"))
            {
                ListInterviews.Sort(delegate(Interview i1, Interview i2)
                {
                    return i1.IdInterview.CompareTo(i2.IdInterview);
                });
            }
            else
            {
                ListInterviews.Sort(delegate(Interview i2, Interview i1)
                {
                    return i1.IdInterview.CompareTo(i2.IdInterview);
                });
            }
        }

        if (SortExpression.Equals("RegisterDate"))
        {
            if (HiddenFieldSortDirection.Value.Equals("Ascending"))
            {
                ListInterviews.Sort(delegate(Interview i1, Interview i2)
                {
                    return i1.RegisterDate.Value.CompareTo(i2.RegisterDate.Value);
                });
            }
            else
            {
                ListInterviews.Sort(delegate(Interview i2, Interview i1)
                {
                    return i1.RegisterDate.Value.CompareTo(i2.RegisterDate.Value);
                });
            }
        }


        if (SortExpression.Equals("PromoterDateBirth"))
        {
            if (HiddenFieldSortDirection.Value.Equals("Ascending"))
            {
                ListInterviews.Sort(delegate(Interview i1, Interview i2)
                {
                    return ((i1.PromoterDateBirth.HasValue)?i1.PromoterDateBirth.Value: (new DateTime())).CompareTo(
                        (i2.PromoterDateBirth.HasValue)?i2.PromoterDateBirth.Value: (new DateTime()));
                });
            }
            else
            {
                ListInterviews.Sort(delegate(Interview i2, Interview i1)
                {
                    return ((i1.PromoterDateBirth.HasValue) ? i1.PromoterDateBirth.Value : (new DateTime())).CompareTo(
                        (i2.PromoterDateBirth.HasValue) ? i2.PromoterDateBirth.Value : (new DateTime()));
                });
            }
        }
        
        HiddenFieldSortExpression.Value = SortExpression;

        return ListInterviews;
    }

    protected void GridViewInterview_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (HiddenFieldSortExpression.Value.Equals(e.SortExpression) && HiddenFieldSortDirection.Value.Equals("Ascending"))
        {
            HiddenFieldSortDirection.Value = "Descending";
        }
        else
        {
            HiddenFieldSortDirection.Value = "Ascending";
        }

        GridViewInterview.DataSource = GetListFilterInterviews(e.SortExpression, HiddenFieldSortDirection.Value);
        GridViewInterview.DataBind();
    }

    protected void GridViewInterview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewInterview.DataSource = GetListFilterInterviews(HiddenFieldSortExpression.Value, HiddenFieldSortDirection.Value);
        GridViewInterview.PageIndex = e.NewPageIndex;
        GridViewInterview.DataBind();
    }


}
